<template>
    <div id="registerForm">
        <flex-step :total="3" :current="step" />
        <div id="step1" class="steps" :style="{display: step === 1 ? 'block' : 'none'}" :class="step === 1 ? 'showWindow' : ''">
            <div class="title">User Register</div>
            <div class="inputLine less">
                <div class="label">User ID:</div>
                <input type="text" id="username" name="username" v-model="account.username" />
                <div id="usernameMessage" :style="{opacity: message === '' ? 0 : 1}">{{ message }}</div>
            </div>
            <div class="inputLine less">
                <div class="label">New password:</div>
                <input type="password" name="password" v-model="account.password" />
            </div>
            <div class="inputLine less">
                <div class="label">Repeat password:</div>
                <input type="password" name="repeatedPassword" v-model="account.repeatPassword" />
            </div>
            <div class="button" @click="handleNext">Next&nbsp;&gt;</div>
        </div>

        <div id="step2" class="steps" :style="{display: step === 2 ? 'block' : 'none'}" :class="step === 2 ? 'showWindow' : ''">
            <div class="title long">Account Information1</div>
            <div class="inputLine">
                <div class="label">First name:</div>
                <input type="text" name="firstName" v-model="account.firstName" />
            </div>
            <div class="inputLine">
                <div class="label">Last name:</div>
                <input type="text" name="lastName" v-model="account.lastName " />
            </div>
            <div class="inputLine">
                <div class="label">Email:</div>
                <input type="text" size="40" name="email" v-model="account.email" />
            </div>
            <div class="inputLine">
                <div class="label">Phone:</div>
                <input type="text" name="phone" v-model="account.phone" />
            </div>
            <div class="inputLine">
                <div class="label">Address 1:</div>
                <input type="text" size="40" name="address1" v-model="account.address1" />
            </div>
            <div class="button more" @click="step = 1">&lt;&nbsp;Previous</div><!--空白
            --><div class="button more" @click="step = 3">Next&nbsp;&gt;</div>
        </div>

        <div id="step3" class="steps" :style="{display: step === 3 ? 'block' : 'none'}" :class="step === 3 ? 'showWindow' : ''">
            <div class="title long">Account Information2</div>
            <div class="inputLine">
                <div class="label">Address 2:</div>
                <input type="text" size="40" name="address2" v-model="account.address2" />
            </div>
            <div class="inputLine">
                <div class="label">City:</div>
                <input type="text" name="city" v-model="account.city" />
            </div>
            <div class="inputLine">
                <div class="label">State:</div>
                <input type="text" size="4" name="state" v-model="account.state" />
            </div>
            <div class="inputLine">
                <div class="label">Zip:</div>
                <input type="text" size="10" name="zip" v-model="account.zip" />
            </div>
            <div class="inputLine">
                <div class="label">Country:</div>
                <input type="text" size="15" name="country" v-model="account.country" />
            </div>
            <div class="button more" @click="step = 2">&lt;&nbsp;Previous</div><!--
            --><div class="button more" id="registerSubmit" @click="submit">Submit</div>
        </div>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
import FlexStep from '@/components/FlexStep.vue'

export default {
    components: {
        FlexStep
    },
    data() {
        return {
            step: 1,
            account: {
                username: '',
                password: '',
                repeatPassword: '',
                firstName: '',
                lastName: '',
                email: '',
                phone: '',
                address1: '',
                address2: '',
                city: '',
                state: '',
                zip: '',
                country: ''
            },
            message: ''
        }
    },
    methods: {
        ...mapActions({
            getAccountByUsername: 'account/getAccountByUsername',
            register: 'account/register',
            getAccountByUsernameAndPassword: 'account/getAccountByUsernameAndPassword'
        }),
        submit() {
            this.register(this.account).then((res) => {
                this.getAccountByUsernameAndPassword({username: this.account.username, password: this.account.password}).then((res) => {
                    this.$router.push('/')
                }, (error) => {console.log(error)})
            }, (error) => {
                console.log(error)
            })
        },
        handleNext() {
            this.getAccountByUsername(this.account.username).then((res) => {
                this.message = '用户名已存在'
                return
            }, (error) => {
                this.step = 2
            })
        }
    }
}
</script>

<style lang="scss" scoped>
#registerForm
{
    width: 100%;
    height: 780px;
    position: relative;
}

@keyframes show
{
    0%
    {
        opacity: 0;
        transform: translateY(-10px);
    }
    100%
    {
        opacity: 1;
        transform: translateY(0px);
    }
}

.showWindow {
    animation: show 0.3s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
}

.steps
{
    color: #833d1e;
    width: 45%;
    height: 621px;
    position: absolute;
    top: 70px;
    left: 27.5%;
    border-radius: 3px;
    background-color: #efe7db;
    box-shadow: 2px 2px 15px #78a686;
    padding: 1px;
    overflow: hidden;
    display: none;
}

.steps>.title
{
    border: none;
    border-bottom: 3px solid #833d1e;
    text-align: center;
    overflow: hidden;
    width: 30%;
    margin: 35px auto 0 auto;
    font-size: 2em;
    color: #833d1e;
}

.steps>.title.long
{
    width: 45%;
}

.inputLine
{
    width: 100%;
    height: 50px;
    overflow: hidden;
    margin-top: 25px;
    margin-left: 0;
    margin-right: 0;
}

.inputLine.less
{
    margin-top: 55px;
}

.inputLine>.label
{
    float: left;
    position: relative;
    height: 26px;
    top: 24px;
    font-size: 1.3em;
    width: 23%;
    margin: 0 0 0 15%;
}

.inputLine>input
{
    outline: none;
    width: 36%;
    border: none;
    color: #833d1e;
    border-bottom: 3px solid #833d1e;
    height: 30px;
    position: relative;
    top: 15px;
    font-size: 1.1em;
    float: left;
    background: transparent;
}

#step2 input, #step3 input
{
    width: 45%;
}

.steps .button
{
    cursor: pointer;
    text-align: center;
    line-height: 35px;
    transition: all 0.3s cubic-bezier(0.55, 0.085, 0.68, 0.53);
    border: 3px solid #507e64;
    color: #507e64;
    width: 250px;
    height: 40px;
    margin: 80px auto 50px auto;
    border-radius: 8px;
    font-size: 1.1em;
}

.steps .button:hover
{
    background-color: #507e64;
    color: #efe7db;
}

.steps .button.more
{
    width: 20%;
    float: left;
    margin-left: 20%;
}

#usernameMessage
{
    position: relative;
    top: 24px;
    height: 26px;
    width: 150px;
    float: left;
    margin-left: 15px;
    transition: all 0.5s cubic-bezier(0.55, 0.085, 0.68, 0.53);
    opacity: 0;
    font-size: 1.1em;
}

@keyframes errorButton1
{
    0% {}
    100%
    {
        background-color: #833d1e;
        border: 3px solid #833d1e;
        color: #efe7db;
    }
}

@keyframes errorButton2
{
    0%
    {
        transform: translateX(0);
    }
    20%
    {
        transform: translateX(10px);
    }
    40%
    {
        transform: translateX(-10px);
    }
    60%
    {
        transform: translateX(10px);
    }
    80%
    {
        transform: translateX(-10px);
    }
    100%
    {
        transform: translateX(0);
    }
}

@keyframes errorButton3
{
    0%
    {
        background-color: #833d1e;
        color: #efe7db;
    }
    100%
    {
        background-color: transparent;
        border: 3px solid #507e64;
        color: #507e64;
    }
}

.errorButton
{
    animation: errorButton1 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards, errorButton2 0.8s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards 0.5s, errorButton3 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards 1.3s;
}

@keyframes waiting1
{
    0%
    {
        opacity: 1;
    }
    100%
    {
        opacity: 0;
    }
}

@keyframes waiting2
{
    0%
    {
        width: 20%;
        border-radius: 8px;
    }
    100%
    {
        width: 40px;
        border: 5px solid #a0ceb4;
        border-top-color: #507e64;
        border-radius: 50%;
        margin-left: calc(30% - 20px);
    }
}

@keyframes waiting3
{
    0%
    {
        transform: rotate(0deg);
    }
    100%
    {
        transform: rotate(360deg);
    }
}

@keyframes waiting4
{
    0%
    {
        opacity: 0;
    }
    100%
    {
        opacity: 1;
    }
}

.waiting.button
{
    cursor: default;
    background-color: transparent;
    animation: waiting1 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards, waiting2 50ms linear forwards 0.2s, waiting3 1s linear infinite 0.3s, waiting4 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards 0.35s ;
}

.waiting.button:hover
{
    background-color: transparent;
}

@keyframes success
{
    0%
    {
        background-color: #507e64;
        color: #efe7db;
    }
    100%
    {
        background-color: transparent;
        color: #507e64;
    }
}

.success
{
    animation: success 0.3s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
}
</style>